Skip to content

Refresh the session once for concurrent callers, and smaller review fixes - #7

Merged
jasonpatrickellykrause merged 2 commits into
mainfrom
fix/single-flight-token-refresh
Nov 16, 2026
Merged

jasonpatrickellykrause merged 2 commits into
mainfrom
fix/single-flight-token-refresh

Conversation

@jasonpatrickellykrause

Copy link
Copy Markdown
Collaborator

Single-flight token refresh (review item 7)

When the session expired during concurrent downloads (in the plugin, two albums downloading at once share one client), every in-flight call refreshed the token separately, so "retry once" became one deezer.getUserData call per request at the same moment. That burst is what the 1.2.3 retry limit was meant to prevent.

SetToken now holds a SemaphoreSlim and tracks a token version. GWApi.Call and GetTrackUrl record the version their request used; when Deezer rejects it, SetToken skips the refresh if another caller already refreshed after that request was sent. SetARL passes force: true so a new ARL always gets its own session. deezer.getUserData itself never refreshes, so the lock can't deadlock.

New tests in TokenRefreshTests use a fake HttpMessageHandler:

  • 5 concurrent stale refreshes make one getUserData call.
  • Forced refreshes always call.
  • A refresh for a token that was already replaced is skipped.
  • 3 concurrent GetTrack calls rejected with VALID_TOKEN_REQUIRED cause one refresh and then succeed. This test takes about 2 seconds because of the existing retry delay.

GetTrackUrl also throws InvalidARLException when no ARL is set, instead of a NullReferenceException (item 10).

Smaller fixes

  • HttpResponseMessage is disposed in GWApi.Call, PublicApi.Call, GetTrackUrl, the LRCLIB and MusicBrainz lookups, and UnshortenURL.
  • UnshortenURL accepts any 3xx redirect.
  • MusicBrainz lookups and the tagging block no longer swallow cancellation.
  • EscapeSearch escapes backslashes before quotes.
  • ApplyMetadataToTrackStream reads all four magic bytes across short reads.
  • RemoveSongsFromPlaylist/RemoveSongFromPlaylist pass their cancellation token. Adding the optional parameter is source-compatible but changes the binary signature; nothing outside this repo calls it as a compiled package.
  • The MusicBrainz user agent reads its version from the assembly.

Left as is: GetLowerFallbackBitrate(MP3_128) returns MP3_320. The name says "lower", but changing it would remove the plugin's only fallback for 128 kbps requests.

Bumps the version to 1.2.6 with a changelog entry.

🤖 Generated with Claude Code

When the session expired during concurrent downloads, every in-flight
call refreshed the token separately, so "retry once" became one
deezer.getUserData call per request at the same moment. SetToken now
holds a lock and tracks a token version: a caller whose request used an
older token reuses a refresh that finished after its request was sent.
SetARL forces a refresh so a new ARL always gets its own session.

GetTrackUrl also throws InvalidARLException when no ARL is set, instead
of a NullReferenceException.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- Dispose HttpResponseMessage in GWApi, PublicApi, GetTrackUrl, LRCLIB,
  MusicBrainz, and UnshortenURL.
- UnshortenURL accepts any 3xx redirect, not only 302.
- MusicBrainz lookups and the tagging block no longer swallow
  cancellation.
- EscapeSearch escapes backslashes before quotes.
- ApplyMetadataToTrackStream reads all four magic bytes across short reads.
- RemoveSongs(s)FromPlaylist pass their cancellation token.
- The MusicBrainz user agent reads its version from the assembly.
- Bump to 1.2.6.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@jasonpatrickellykrause
jasonpatrickellykrause merged commit d0c4046 into main Nov 16, 2026
5 checks passed
@jasonpatrickellykrause
jasonpatrickellykrause deleted the fix/single-flight-token-refresh branch November 16, 2026 21:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant